<?php

namespace App\Http\Controllers\H5;

use App\Library\Mall\AuctionTrait;
use App\Models\Item;
use App\Models\Order;
use Illuminate\Http\Request;

class AuctionController extends BaseController
{
    use AuctionTrait;

    public function buyNow(Request $request)
    {
        $itemid = intval($request->input('itemid'));
        $quantity = intval($request->input('quantity'));

        $item = Item::find($itemid);
        if ($item) {
            $total_fee = floatval($item->price) * $quantity;

            $shipping = $this->user()->addresses()->orderByDesc('isdefault')->first();
            return $this->view('h5.auction.buynow', compact('itemid', 'quantity', 'item', 'total_fee', 'shipping'));
        } else {
            return false;
        }
    }

    public function createOrder(Request $request)
    {
        return $this->buildSingleOrder($request);
    }

    public function buildOrderSuccess(Order $order)
    {
        return redirect('h5/bought/detail?order_id='.$order->order_id);
    }

    public function itemNotFondResponse()
    {
        return $this->view('h5.auction.buynow');
    }

    public function confirmOrder(Request $request)
    {
        $itemIds = $request->input('items');

        $total_fee = 0;
        $total_count = 0;
        $items = $this->user->cartItems()->whereIn('itemid', $itemIds)->get()->map(function ($item) use (&$total_fee){
            $total_fee+= $item->price * $item->quantity;
            return $item;
        });

        $shipping = $this->user()->addresses()->orderByDesc('isdefault')->first();

        return $this->view('h5.auction.confirmorder', compact('itemIds', 'items', 'shipping', 'total_fee'));
    }

    protected function settlementResponse()
    {
        return redirect('h5/bought');
    }
}
